package com.yyjj.my.activity;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.aries.ui.view.title.TitleBarView;
import com.vise.xsnow.event.BusManager;
import com.vise.xsnow.http.ViseHttp;
import com.vise.xsnow.http.callback.ACallback;
import com.vise.xsnow.http.request.PostRequest;
import com.yyjj.my.R;
import com.yyjj.my.api.UrlContant;
import com.yyjj.my.entity.UpDataUserInfoEntity;

import tech.com.commoncore.base.BaseTitleActivity;
import tech.com.commoncore.basecomponent.ServiceFactory;
import tech.com.commoncore.constant.ApiConstant;
import tech.com.commoncore.event.PersonalChangeEvent;
import tech.com.commoncore.utils.OnNoDoubleClickListener;
import tech.com.commoncore.utils.ToastUtil;

public class LabelActivity extends BaseTitleActivity {
    TitleBarView titleBarHeadFastLib;
    EditText labelEd;
    Button labelBtn;

    @Override
    public void setTitleBar(TitleBarView titleBar) {
        titleBar.setTitleMainText("编辑标签");
    }

    @Override
    public int getContentLayout() {
        return R.layout.my_activity_lable;
    }


    private void updateUserInfo(String url, String user_nickname, String signature) {
        showLoading();
        PostRequest postRequest = ViseHttp.POST(UrlContant.UPDATA_USERINFO)
                .baseUrl(ApiConstant.BASE_URL)
                .addHeader("XX-Token", ServiceFactory.getInstance().getLoginService().getCurrentUser().getToken())
                .addHeader("XX-Device-Type", "android");
        if (!url.equals("")) postRequest.addParam("avatar", url);
        if (!user_nickname.equals("")) postRequest.addParam("user_nickname", user_nickname);
        if (!signature.equals("")) postRequest.addParam("signature", signature);
        postRequest.request(new ACallback<UpDataUserInfoEntity>() {

            @Override
            public void onSuccess(UpDataUserInfoEntity data) {
                hideLoading();
                ToastUtil.show(data.getMsg());
                if (data.getCode() == 1) {
                    BusManager.getBus().post(new PersonalChangeEvent());
                }
                finish();
            }

            @Override
            public void onFail(int errCode, String errMsg) {
                Log.d("BaseEntity", errMsg);
                hideLoading();
            }
        });
    }

    @Override
    public void initView(Bundle savedInstanceState) {
        titleBarHeadFastLib = findViewById(R.id.titleBar_headFastLib);
        labelEd = findViewById(R.id.label_ed);
        labelBtn = findViewById(R.id.label_btn);
        labelBtn.setOnClickListener(new OnNoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View v) {
                String signature = labelEd.getText().toString();
                if (signature.equals("")) {
                    ToastUtil.show("请输入标签");
                    return;
                }
                updateUserInfo("", "", signature);
            }
        });
    }

}
